﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace QDPay.Bus.util
{
    public class LogUtil
    {
        private static readonly object writeFile = new object();
        public static void WriteLog(string text)
        {
            WriteLog("", text);
        }

        public static void WriteLog(string fileNamePrefix, string text)
        {
            lock (writeFile)
            {
                FileStream fs = null;
                StreamWriter sw = null;
                try
                {
                    string filename = fileNamePrefix + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                    //服务器中日志目录
                    string folder = GetMapPath("/Log/");
                    if (!Directory.Exists(folder))
                        Directory.CreateDirectory(folder);
                    fs = new FileStream(folder + "/" + filename, FileMode.Append, FileAccess.Write);
                    sw = new StreamWriter(fs, Encoding.UTF8);
                    sw.WriteLine(DateTime.Now.ToString() + "     " + text + "\r\n");
                }
                finally
                {
                    if (sw != null)
                    {
                        sw.Flush();
                        sw.Dispose();
                        sw = null;
                    }
                    if (fs != null)
                    {
                        //     fs.Flush();
                        fs.Dispose();
                        fs = null;
                    }
                }
            }
        }

        public static string GetMapPath(string path)
        {
            return AppDomain.CurrentDomain.BaseDirectory + path;
        }
    }
}
